<?php

class User extends CActiveRecord
{
    
    
        const NOT_MEMBER = 2;
        const MEMBER     = 1;      
	/**
	 * The followings are the available columns in table 'tbl_user':
	 * @var integer $id
	 * @var string $username
	 * @var string $password
	 * @var string $salt
	 * @var string $email
	 * @var string $profile
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{user}}';
	}

	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('username, password, classe, email', 'required'),
			array('username, password,img_detail,party, classe, email, salt', 'length', 'max'=>128),
			array('profile', 'safe'),
                        array('img', 'file', 'types'=>'jpg, gif, png'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, username,classe, email,img,party, profile', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			//'posts' => array(self::HAS_MANY, 'Post', 'author_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'username' => 'Nome',
			'password' => 'Password',
			'salt' => 'Salt',
			'email' => 'Email',
			'profile' => 'Descrição',
                        'classe'=> 'Classe',
                        'party'=>'Membro',
                        'img'=>'Foto',
                        'img_detail'=>'Titulo da foto',
		);
	}

	public function loadUsers()
	{

   	      $users =$this->findAll();
              

	  return $users;
	}

	public function loadUsersMenu()
	{
          $ar = array();
           foreach($this->loadUsers() as $users){
               $ar[] = array('label'=>$users['username'], 'url'=>array('user/profile&id='.$users['id']), 'linkOptions' => array( 'class' => 'menulink' ));
           }
              

	  return $ar;
	}

	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('username',$this->username,true);

		$criteria->compare('password',$this->password,true);

		$criteria->compare('email',$this->email,true);

                $criteria->compare('classe',$this->classe,true);

		$criteria->compare('profile',$this->profile,true);
                
                $criteria->compare('img',$this->img,true);
                
		$criteria->compare('party',$this->party,true);
                
		return new CActiveDataProvider('user', array(
			'criteria'=>$criteria,
		));
	}        

	/**
	 * Checks if the given password is correct.
	 * @param string the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($password)
	{
		return $this->hashPassword($password,$this->salt)===$this->password;
	}

	/**
	 * Generates the password hash.
	 * @param string password
	 * @param string salt
	 * @return string hash
	 */
	public function hashPassword($password,$salt)
	{
		return md5($salt.$password);
	}

	/**
	 * Generates a salt that can be used to generate a password hash.
	 * @return string the salt
	 */
	public function generateSalt()
	{
		return uniqid('',true);
	}
              
}